const path = require("path")
const webpack = require("webpack")

const MiniCssExtractPlugin = require("mini-css-extract-plugin")
const babelConfig = require("./babel.config")

const copyWebpackPlugin = require("copy-webpack-plugin")
const TerserPlugin = require("terser-webpack-plugin")
const UglifyJsPlugin = require("uglifyjs-webpack-plugin")
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin")

module.exports = {
  //入口文件的配置项
  entry: {
    www: "./bin/www"
  },
  mode: "production",
  //出口文件的配置项
  output: {
    path: path.resolve(__dirname, "./dist"), // node语法
    filename: "[name].js" // 这里的[name]代表不改变原先文件的名称
  },
  target: "node",
  node: {
    console: true,
    global: true,
    process: true,
    Buffer: true,
    __filename: true,
    __dirname: true,
    setImmediate: true,
    path: true
  },
  //模块：例如解读CSS,图片如何转换，压缩
  module: {
    rules: [
      {
        test: /\.(jsx|js)$/,
        use: {
          loader: "babel-loader"
        },
        exclude: /node_modules/
      },
      {
        test: /\.(css|less|scss)$/,
        use: [
          // devMode ? 'style-loader' : MiniCssExtractPlugin.loader,
          // 'style-loader',
          MiniCssExtractPlugin.loader,
          "css-loader",
          // 'postcss-loader',
          {
            loader: "postcss-loader",
            options: {
              plugins: () => {
                return [require("autoprefixer")]
              }
            }
          },
          // "less-loader",
          "css-loader"
        ]
      }
    ]
  },
  //插件，用于生产模版和各项功能
  plugins: [
    new webpack.ProvidePlugin({
      // onerror: "koa-onerror",
      // views: "koa-views"
    }),
    new MiniCssExtractPlugin({
      filename: "[name].css"
    }),
    // new MiniCssExtractPlugin({
    //   filename: "css/[name].css"
    // }),
    new webpack.NormalModuleReplacementPlugin(/^any-promise$/, "bluebird"),
    new copyWebpackPlugin([
      {
        from: __dirname + "/public",
        to: "./public"
      }
    ])
  ],
  //配置webpack开发服务功能
  devServer: {},
  optimization: {
    // splitChunks: {
    //   cacheGroups: {
    //     commons: {
    //       test: /[\\/]node_modules[\\/]/,
    //       name: "vendor",
    //       chunks: "all"
    //     }
    //   }
    // },
    minimizer: [
      // new UglifyJsPlugin({
      //   cache: true,
      //   parallel: true
      // }),
      new TerserPlugin(),
      new OptimizeCSSAssetsPlugin({})
    ]
  }
}
